package com.itcat.controller;

import com.itcat.common.CommonPage;
import com.itcat.common.CommonResult;
import com.itcat.mysql.param.PmsProductParam;
import com.itcat.mysql.pojo.PmsProduct;
import com.itcat.service.PmsProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.stylesheets.LinkStyle;

import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-02 15:58
 */
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    private PmsProductService pmsProductService;

    /**
     * 创建商品
     *
     * @param pmsProductParam
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody PmsProductParam pmsProductParam) {
//        System.out.println(pmsProductParam);
        pmsProductService.create(pmsProductParam);
        return CommonResult.success();
    }


    /**
     * 分页查询商品信息
     *
     * @param pmsProductParam
     * @return
     */
    @GetMapping("/list")
    public CommonResult<CommonPage<PmsProduct>> list(PmsProductParam pmsProductParam) {
        CommonPage<PmsProduct> page = pmsProductService.list(pmsProductParam);
        return CommonResult.success(page);
    }


    /**
     * 批量修改删除状态
     *
     * @return
     */
    @PostMapping("/update/deleteStatus")
    public CommonResult updateDeleteStatus(Integer[] ids, Integer deleteStatus) {
        pmsProductService.updateDeleteStatus(ids, deleteStatus);
        return CommonResult.success();
    }

    /**
     * 批量设为新品
     *
     * @return
     */
    @PostMapping("/update/newStatus")
    public CommonResult updateNewStatus(Integer[] ids, Integer newStatus) {
        pmsProductService.updateNewStatus(ids, newStatus);
        return CommonResult.success();
    }

    /**
     * 批量上下架商品
     *
     * @return
     */
    @PostMapping("/update/publishStatus")
    public CommonResult updatePublishStatus(Integer[] ids, Integer publishStatus) {
        pmsProductService.updatePublishStatus(ids, publishStatus);
        return CommonResult.success();
    }

    /**
     * 批量推荐商品
     *
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult updateRecommendStatus(Integer[] ids, Integer recommendStatus) {
        pmsProductService.updateRecommendStatus(ids, recommendStatus);
        return CommonResult.success();
    }


    /**
     * 根据商品id获取商品编辑信息
     *
     * @param id
     * @return
     */
    @GetMapping("/updateInfo/{id}")
    public CommonResult updateInfo(@PathVariable("id") Integer id) {
        PmsProductParam pmsProductParam = pmsProductService.findProductById(id);
        return CommonResult.success(pmsProductParam);
    }


    /**
     * 更新商品
     *
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable("id") Long id, @RequestBody PmsProductParam pmsProductParam) {
        pmsProductService.update(id, pmsProductParam);
        return CommonResult.success();
    }
}
